package com.xbongbong.pro.message.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.wrap.Entity;
import com.xbongbong.pro.message.constant.MessageSetDetailTitleConstants;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.MessageSetDetailPojo;
import com.xbongbong.pro.message.pojo.dto.MessageSetDetailDTO;
import com.xbongbong.pro.message.pojo.vo.MessageSetDetailVO;
import com.xbongbong.pro.message.pojo.vo.PushSetVO;
import com.xbongbong.pro.message.pushset.OrderPushSet;
import com.xbongbong.saas.domain.entity.ext.ContractEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.OrderEnum;
import com.xbongbong.saas.model.ContractUserModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 订单中心消息推送设置实现类
 *
 * @author LiZhikang
 * @date 2022/11/5 17:18
 */
@Service
public class OrderCenterMessageSetServiceImpl extends AbstractMessageSetService {

    @Resource
    private ContractUserModel contractUserModel;


    @Override
    public List<String> alias() {
        return Collections.singletonList(XbbRefTypeEnum.ORDER_CENTER.getAlias());
    }

    @Override
    public String initSetDetailList() throws Exception {
        return null;
    }

    @Override
    public MessageSetDetailVO getSetDetailList(MessageSetDetailDTO messageSetDetailDTO) throws Exception {
        List<MessageSetDetailPojo> messageSetDetailPojos = new ArrayList<>();
        Integer typeInt = messageSetDetailDTO.getSaasIds().get(0).intValue();
        typeInt = XbbRefTypeEnum.transferBusinessType4Distributor(typeInt, messageSetDetailDTO.getDistributorMark());
        String alias = PushTypeEnum.getByCode(typeInt).getAlias();
        if (!Objects.equals(alias, PushTypeEnum.UNKNOWN.getAlias())) {
            String methodName = "getMessageSetSaas" + alias.substring(0, 1).toUpperCase() + alias.substring(1) + "DetailList";
            Method method = this.getClass().getDeclaredMethod(methodName, String.class, List.class, Integer.class);
            method.invoke(this, messageSetDetailDTO.getCorpid(), messageSetDetailPojos, typeInt);
        }
        return new MessageSetDetailVO(messageSetDetailPojos);
    }

    /**
     * 订货单 9510
     *
     * @param corpid
     * @param messageSetDetailPojos
     * @param typeInt
     * @throws IllegalAccessException
     * @throws XbbException
     * @throws InstantiationException
     */
    private void getMessageSetSaasOrderPushDetailList(String corpid, List<MessageSetDetailPojo> messageSetDetailPojos, Integer typeInt) throws IllegalAccessException, XbbException, InstantiationException {
        List<PushTypeEnum> pushTypeEnumList = new ArrayList<>(PushTypeEnum.getAllEnumByType(typeInt));
        List<PushTypeEnum> basePushTypeEnum = new ArrayList<>();
        for (PushTypeEnum pushTypeEnum : pushTypeEnumList) {
            if (Objects.equals(pushTypeEnum.getSubCode(), PushTypeEnum.ORDER_ADD_PUSH.getSubCode()) || Objects.equals(pushTypeEnum.getSubCode(), PushTypeEnum.ORDER_CHANGE_PUSH.getSubCode()) || Objects.equals(pushTypeEnum.getSubCode(), PushTypeEnum.ORDER_TEAM_ADD_DEL_MAIN_PUSH.getSubCode()) || Objects.equals(pushTypeEnum.getSubCode(), PushTypeEnum.ORDER_TEAM_ADD_DEL_SYNERGETIC_PUSH.getSubCode())) {
                basePushTypeEnum.add(pushTypeEnum);
            }
        }

        OrderPushSet orderPushSet = new OrderPushSet();
        MessageSetDetailPojo detailPojo = new MessageSetDetailPojo();
        detailPojo.setTitle(I18nMessageUtil.getMessage(MessageSetDetailTitleConstants.ORDER_BASE));
        List<PushSetEntity> basePushTypeList = new ArrayList<>();
        for (PushTypeEnum pushTypeEnum : basePushTypeEnum) {
            PushSetEntity pushSetEntity = orderPushSet.getPushSet(corpid, pushTypeEnum.getSubCode(), pushSetModel);
            basePushTypeList.add(pushSetEntity);
        }
        List<PushSetVO> pushSetVOList = new ArrayList<>();
        BeanUtil.copyPropertiesList(basePushTypeList, pushSetVOList, PushSetVO.class);
        detailPojo.setValue(pushSetVOList);
        messageSetDetailPojos.add(detailPojo);
    }

    @Override
    public List<String> getUserIdListByPushSetRule(String corpid, Integer type, Integer subType, UserEntity userEntity, Entity entity) {
        return distinct(getOrderUserIdList(corpid, subType, userEntity, entity));
    }

    private List<String> getOrderUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        ContractEntityExt contractEntity = (ContractEntityExt) entity;
        OrderPushSet contractPushSet = new OrderPushSet();
        PushSetEntity pushSetEntity = contractPushSet.getPushSet(corpid, subType, pushSetModel);
        Long contractId = contractEntity.getId();

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (contractId != null && contractId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractEntity.getId(), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractEntity.getId(), 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.ORDER_SIGNING.getAlias())) {
                            JSONObject contractDataObject = JsonHelperUtil.getJSONObject(contractEntity.getData());
                            result.add(contractDataObject.getString(OrderEnum.SIGN_PERSON.getAttr()));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
}
